var saleBillData;
$(function () {
    //js方式创建
    $("#stockOutcomeBill_datagrid").datagrid({
        width: 800,
        height: 400,
        //数据来源url
        //url: '/stockOutcomeBill/list?billStatus=1',
        //列
        columns: [[
            //添加一列复选框
            {field: 'x', checkbox: true},
            {field: 'id', title: '编号', hidden: true},
            {field: 'sn', title: '小票单号', width: 80},
            {field: 'inputTime', title: '销售时间', width: 150, sortable: true},
            {field: 'totalNumber', title: '总数量', width: 50},
            {field: 'totalAmount', title: '总金额', width: 80},
            {field: 'discount', title: '折扣', width: 50},
            {field: 'discountAmount', title: '折扣金额', width: 80},
            {field: 'receivables', title: '应收金额', width: 80},
            {field: 'paykind', title: '支付方式', width: 50},
            {
                field: 'member', title: '消费对象', width: 50, formatter: function (value, row, index) {
                return value ?value.name : "散客";
            }
            },
            {
                field: 'operator', title: '收银人员', width: 50, formatter: function (value, row, index) {
                return value.realname;
            }
            },
            {
                field: 'op', title: '操作', width: 100, formatter: function (value, row, index) {
                return "<a id='btSaleReturn' onclick='showItem()'>明细退货</a>&nbsp;&nbsp;<a id='btSaleReturn' onclick='saleBillReturn()'>退单</a>";
            }
            }

        ]],
        //按比例分配列宽以适应表格大小
        fitColumns: true,
        fit: true,
        toolbar: "#tb",
        //斑马线效果
        striped: true,
        //底部分页栏
        pagination: true,
        //显示行号列
        rownumbers: true,
        //只能选择单行数据
        singleSelect: true,
        //指定页面大小的可选数值
        pageList: [10, 15, 20],
        //指定默认的页面大小
        pagesize: 10,
        onLoadSuccess: function (data) {
            saleBillData = $.extend(true, {}, data);
        }

    });

    //操作员下拉框
    $('#operator').combobox({
        url: '/employee/allEmps',
        valueField: 'id',
        textField: 'realname',
        panelHeight: 'auto'

    });

    //初始化小票明细退货的dialog
    $("#stockOutcomeBillItem_dialog").dialog({
        width: 900,
        height: 500,
        title: '小票明细表',
        draggable: true,
        resizable: true,
        closed: true,
        onClose: function () {
            $("#stockOutcomeBill_datagrid").datagrid("load");
        }
    })

    //小票明细表格
    $("#stockOutcomeBillItem_datagrid").datagrid({
        width: 800,
        height: 400,
        //url: '/stockOutcomeBill/billItem?billId=1',
        //列
        columns: [[
            //添加一列复选框
            {field: 'billId', title: '小票id', hidden: true, width: 100},
            {
                field: 'product', title: '商品名称', width: 100, formatter: function (value, row, index) {
                return value.name;
            }
            },
            {field: 'billSn', title: '小票编号', width: 100},
            {field: 'salePrice', title: '单价', width: 100},
            {field: 'number', title: '数量', width: 100},
            {field: 'discount', title: '折扣', width: 100},
            {field: 'amount', title: '金额', width: 100},
            {field: 'itemStatus', title: '状态', width: 100},
            {
                field: 'op', title: '操作', width: 100, formatter: function (value, row, index) {
                return "<a id='btSaleItemReturn' onclick='saleItemReturn()'>确认退货</a>";
            }
            }
        ]],
        //按比例分配列宽以适应表格大小
        fitColumns: true,
        fit: true,
        //斑马线效果
        striped: true,
        //底部分页栏
        pagination: true,
        //显示行号列
        rownumbers: true,
        //只能选择单行数据
        singleSelect: true,
        //指定页面大小的可选数值
        pageList: [10, 15, 20],
        //指定默认的页面大小
        pagesize: 10
    });

    //页面加载完执行today()
    today();

});

//小票明细弹出方法
function showItem() {
    var row = $("#stockOutcomeBill_datagrid").datagrid("getSelected");
    //将发送ajax请求该小票对应的明细信息,载入明细数据,并弹出小票明细框,
    $("#stockOutcomeBillItem_datagrid").datagrid("load", "/stockOutcomeBill/billItem?billId=" + row.id);
    $("#stockOutcomeBillItem_dialog").dialog("open");

}

//小票明细退货方法()
function saleItemReturn() {
    $.messager.confirm('确认', '您确认要退货吗？', function (r) {
        if (!r) {
            return;
        } else {
            //发送请求,参数为itemId,简单处理,改变原来订单明细数据的状态
            var row = $("#stockOutcomeBillItem_datagrid").datagrid("getSelected");
            $.post("/stockOutcomeBill/saleReturn", {'billId': row.billId, 'itemId': row.product.id}, function (data) {
                //响应回来是否退货删除明细成功
                if (!data.success) {
                    $.messager.alert("温馨提示", data.errorMsg);
                } else {
                    $.messager.alert("温馨提示", "退货成功");
                    $("#stockOutcomeBillItem_datagrid").datagrid("load");
                }
            })
        }
    });
}


//小票整单退货
function saleBillReturn() {
    $.messager.confirm('确认', '您确认要整单退货吗？', function (r) {
        if (!r) {
            return;
        } else {
            //发送请求,参数为billId,删除整单
            var row = $("#stockOutcomeBill_datagrid").datagrid("getSelected");
            $.post("/stockOutcomeBill/saleReturn", {billId: row.id}, function (data) {
                //响应回来是否退货删除明细成功
                if (!data.success) {
                    $.messager.alert("温馨提示", data.errorMsg);
                } else {
                    $.messager.alert("温馨提示", "退货成功");
                    $("#stockOutcomeBill_datagrid").datagrid("reload");
                }
            });
        }
    });
}

//添加员工
function add() {
    //打开窗口前先执行表单数据的清空
    $("#editForm").form("clear");
    $("#empinput_dialog").dialog("open");
    $("#empinput_dialog").dialog("setTitle", "员工添加");
}

//编辑员工
function edit() {
    //打开窗口前先执行表单数据的清空
    $("#editForm").form("clear");

    //点击编辑,判断是否选中一行数据
    var row = $("#stockOutcomeBill_datagrid").datagrid("getSelected");
    //否,就弹窗提示先选行
    if (!row) {
        $.messager.alert('温馨提示', '请选择一条要编辑的数据!');
    } else {
        //是,就将行数据映射到表单中
        $("#empinput_dialog").dialog("open");
        $("#empinput_dialog").dialog("setTitle", "员工修改");
        if (row.dept) {
            row["dept.id"] = row.dept.id;
        }
        $("#editForm").form('load', row);
    }
}

//保存方法
//因为添加和修改公用一个保存按钮,所以要区分操作
function save() {
    var url = "/stockOutcomeBill/saveOrUpdate";
    $("#editForm").form("submit", {
        url: url,
        success: function (data) {
            var data = $.parseJSON(data);
            //如果失败就弹窗,保留编辑页面
            if (!data.success) {
                $.messager.alert('温馨提示', data.msg);
            } else {
                //成功就关闭编辑窗口,直接刷新表格
                $("#empinput_dialog").dialog("close");
                $("#stockOutcomeBill_datagrid").datagrid("reload");
            }
        }
    })
}


//刷新方法
function reload() {
    $("#stockOutcomeBill_datagrid").datagrid("reload");
}

//取消方法
function cancel() {
    $("#empinput_dialog").dialog("close");
}


//高级查询
function advSearch() {
    //获取到查询参数,easyui组件获取参数都采用easyui里面的方法
    var operatorId = $("#operator").combobox("getValue");
    var beginDate = $("#beginDate").textbox("getValue");
    var endDate = $("#endDate").textbox("getValue");
    //采用带参数刷新表格的方式进行查询
    $('#stockOutcomeBill_datagrid').datagrid('load', {
        operatorId: operatorId,
        beginDate: beginDate,
        endDate: endDate
    });
}

function today() {
    $("#abc").css("visibility", 'hidden');
    var beginDate = new Date(new Date(new Date().toLocaleDateString()).getTime());
    var endDate = new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1);
    beginDate = DateUtils.format(beginDate,"yyyy-MM-dd HH:mm:ss");
    endDate = DateUtils.format(endDate,"yyyy-MM-dd HH:mm:ss");
    $("#stockOutcomeBill_datagrid").datagrid("load","/stockOutcomeBill/list?billStatus=1&beginDate="+beginDate+"&endDate="+endDate);
}
function yesterday() {
    $("#abc").css("visibility", 'hidden');
    var beginDate = new Date(new Date(new Date().toLocaleDateString()).getTime()-+24*60*60*1000);
    var endDate = new Date(new Date(new Date().toLocaleDateString()).getTime()-1);
    beginDate = DateUtils.format(beginDate,"yyyy-MM-dd HH:mm:ss");
    endDate = DateUtils.format(endDate,"yyyy-MM-dd HH:mm:ss");
    $("#stockOutcomeBill_datagrid").datagrid("load","/stockOutcomeBill/list?billStatus=1&beginDate="+beginDate+"&endDate="+endDate);

}
function thisweek() {
    $("#abc").css("visibility", 'hidden');
    var currentDate = new Date(new Date(new Date().toLocaleDateString()).getTime());
    var week = currentDate.getDay();
    var month = currentDate.getDate();
    var millisecond = 1000 * 60 * 60 * 24;
    var minusDay = week != 0 ? week - 1 : 6;
    var monday = new Date(currentDate.getTime() - (minusDay * millisecond));
    var beginDate = DateUtils.format(monday,"yyyy-MM-dd HH:mm:ss");
    var endDate = new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1);
    endDate = DateUtils.format(endDate,"yyyy-MM-dd HH:mm:ss");
    $("#stockOutcomeBill_datagrid").datagrid("load","/stockOutcomeBill/list?billStatus=1&beginDate="+beginDate+"&endDate="+endDate);
    console.log(beginDate);
    console.log(endDate);
}
function otherday() {
    $("#abc").css("visibility", 'visible');
}







